/* eslint-disable no-caller */
import React, { useState } from "react";
import { Button, Modal, Space } from "antd";



const get_function = (data, val) => {
    if (data.length === 0) {
        for (let item in val) {
            if (item < val.length) {
                data.push(val[item]);
            }
        }
    } else {
        //data=[];
        //data.length = 0;
        data.splice(0, data.length);
        for (let item in val) {
            if (item < val.length) {
                data.push(val[item]);
            }
        }
    }
}
const edit_function = (data, value) => {
    if (data.length === 0) {
        data.push(value)
    } else {
        data.splice(0, data.length, value)
    }
}
const search_function = (data, values_data) => {
    if (data.length != 0) {
        data.splice(0, data.length);
        for (let item in values_data) {
            if (item < values_data.length) {
                data.push(values_data[item])
            }
        }
    } else {
        for (let item in values_data) {
            if (item < values_data.length) {
                data.push(values_data[item])
            }
        }
    }
}
const tree_fun = (data, prop_fun) => {
    let new_data;
    if (data != null && data.length > 0) {
        // 为子树增加code
        var materialsModelStack = new stack();
        for (var i = 0; i < data.length; i++) {
            data[i].key = guid();
            materialsModelStack.push(data[i]);
        }
        while (materialsModelStack.length() > 0) {
            var rootMaterialsModel = materialsModelStack.pop();


            // 如果现实数组中没有则加入到显示数组中
            var isLeaf = false;
            for (var i = 0; i < data.length; i++) {
                if (data[i].key == rootMaterialsModel.key) {
                    isLeaf = true;
                    break;
                }
            }
            if (!isLeaf) {
                data.push(rootMaterialsModel);
            }

            if (rootMaterialsModel.ChildCategory != null) {
                // let key;
                for (var i = 0; i < rootMaterialsModel.ChildCategory.length; i++) {
                    rootMaterialsModel.ChildCategory[i].key = guid();
                    rootMaterialsModel.ChildCategory[i].parentKey = rootMaterialsModel.key;

                }
            }
        }
        new_data = materialsModelStack.dataStore;
    }


    var array = [];
    // 开始构建界面
    // 先清空TAB
    var tab_array = new stack();

    // 开始查找和构建数据
    tab_array.push(new_data[0]);
    var json_val = null;
    while (tab_array.length() > 0) {
        var root_jsonVal = tab_array.pop();


        // 是否找到模型下面真正的材料数据
        if (root_jsonVal.key == new_data.key) {
            json_val = root_jsonVal;
            break;
        }

        if (root_jsonVal.ChildCategory != null && root_jsonVal.ChildCategory.length > 0) {
            for (var i = 0; i < root_jsonVal.ChildCategory.length; i++) {
                tab_array.push(root_jsonVal.ChildCategory[i]);
            }
        } else {
            tab_array.push(root_jsonVal);
        }
    }
    tab_array.clear();

    if (json_val.ChildCategory != null) {
        for (var i = 0; i < json_val.ChildCategory.length; i++) {
            tab_array.push(json_val.ChildCategory[i]);
        }
    } else {
        tab_array.push(json_val);
    }

    while (tab_array.length() > 0) {
        var root_modal = tab_array.pop();
        array.push(root_modal);
    }
    // 开始排序
    array = array.sort(
        function (a, b) {
            return a.order - b.order;
        });
    return array

}
function stack() {
    this.dataStore = [];//保存栈内元素，初始化为一个空数组
    this.top = 0;//栈顶位置，初始化为0
    this.push = push;//入栈
    this.pop = pop;//出栈
    this.peek = peek;//查看栈顶元素
    this.clear = clear;//清空栈
    this.length = length;//栈内存放元素的个数

    function push(element) {
        this.dataStore[this.top++] = element;
    }

    function pop() {
        return this.dataStore[--this.top];
    }

    function peek() {
        return this.dataStore[this.top - 1];
    }

    function clear() {
        this.top = 0;
    }

    function length() {
        return this.top;
    }

}
const tree_new_fun = (new_data, save_fun) => {

    var array = [];
    // 开始构建界面
    // 先清空TAB
    var tab_array = new stack();

    // 开始查找和构建数据
    tab_array.push(new_data.materialsModelJsonValue);
    var json_val = null;
    while (tab_array.length() > 0) {
        var root_jsonVal = tab_array.pop();
        // 是否找到模型下面真正的材料数据


        if (root_jsonVal.categoryName == new_data.modelInfo.name) {
            json_val = root_jsonVal;
            break;
        }


        if (root_jsonVal.childCategory != null && root_jsonVal.childCategory.length > 0) {
            for (var i = 0; i < root_jsonVal.childCategory.length; i++) {
                tab_array.push(root_jsonVal.childCategory[i]);
            }
        } else {
            tab_array.push(root_jsonVal);
        }

    }
    tab_array.clear();

    if (json_val.childCategory != null) {
        for (var i = 0; i < json_val.childCategory.length; i++) {
            tab_array.push(json_val.childCategory[i]);
        }
    } else {
        tab_array.push(json_val);
    }

    while (tab_array.length() > 0) {
        var root_modal = tab_array.pop();
        array.push(root_modal);
    }
    // 开始排序
    array = array.sort(
        function (a, b) {
            return a.order - b.order;
        });
    save_fun(array)
}

// 生产树
const creat_tree = (data) => {
    const array = [];
    for (let item in data) {
        if (item < data.length) {
            array.push(data[item].materialProperty)
        }
    }
    array.map((item, index) => {
        item.key = index;
        item.title = item.name
    })

    var factorial = (function f(num) {
        if (num <= 1) {
            return 1;
        } else {
            return num * f(num - 1);
        }
    })

    const treeObj = (originObj) => {
        //对象深拷贝
        let obj = {};
        for (let key in originObj) {
            var val = originObj[key];
            // eslint-disable-next-line no-caller
            // eslint-disable-next-line no-undef
            obj[key] = typeof val === 'object' ? factorial(val) : val;
        }
        //对象新增children键值，用于存放子树
        obj['children'] = [];
        return obj;
    }

    //data：带转换成树形结构的对象数组
    //attributes：对象属性
    const toTreeData = (data, attributes) => {
        let tree = [];
        //找寻根节点
        for (let i = 0; i < data.length; i++) {
            if (data[i][attributes.parentCode] === '00' || data[i][attributes.parentCode] === null) {
                tree.push(treeObj(data[i]));
                data.splice(i, 1);
                i--;
            }
        }
        //找寻子树
        const run = (chiArr) => {
            if (data.length !== 0) {
                for (let i = 0; i < chiArr.length; i++) {
                    for (let j = 0; j < data.length; j++) {
                        if (chiArr[i][attributes.code] === data[j][attributes.parentCode]) {
                            let obj = treeObj(data[j]);
                            chiArr[i].children.push(obj);
                            data.splice(j, 1);
                            j--;
                        }
                    }
                    run(chiArr[i].children);
                }
            }
        }
        run(tree);
        return tree;
    }
    let attr = {
        code: 'code',
        parentCode: 'parentCode',
    };
    return toTreeData(array, attr)
}
const new_map = (data) => {
    function HashMap() {

        //定义长度
        var length = 0;
        //创建一个对象
        var obj = new Object();

        /**
         * 判断Map是否为空
         */
        this.isEmpty = function () {
            return length == 0;
        };

        /**
         * 判断对象中是否包含给定Key
         */
        this.containsKey = function (key) {
            return (key in obj);
        };

        /**
         * 判断对象中是否包含给定的Value
         */
        this.containsValue = function (value) {
            for (var key in obj) {
                if (obj[key] == value) {
                    return true;
                }
            }
            return false;
        };

        /**
         *向map中添加数据
         */
        this.put = function (key, value) {
            if (!this.containsKey(key)) {
                length++;
            }
            obj[key] = value;
        };

        /**
         * 根据给定的Key获得Value
         */
        this.get = function (key) {
            return this.containsKey(key) ? obj[key] : null;
        };

        /**
         * 根据给定的Key删除一个值
         */
        this.remove = function (key) {
            if (this.containsKey(key) && (delete obj[key])) {
                length--;
            }
        };

        /**
         * 获得Map中的所有Value
         */
        this.values = function () {
            var _values = new Array();
            for (var key in obj) {
                _values.push(obj[key]);
            }
            return _values;
        };

        /**
         * 获得Map中的所有Key
         */
        this.keySet = function () {
            var _keys = new Array();
            for (var key in obj) {
                _keys.push(key);
            }
            return _keys;
        };

        /**
         * 获得Map的长度
         */
        this.size = function () {
            return length;
        };

        /**
         * 清空Map
         */
        this.clear = function () {
            length = 0;
            obj = new Object();
        };
    }
    var tagDataMap = new HashMap()

    for (var i = 0; i < data.length; i++) {
        var eachMaterialsModelJsonValue = data[i];
        var tabData = {};
        tabData.categoryName = eachMaterialsModelJsonValue.categoryName;
        tabData.properties = eachMaterialsModelJsonValue.properties;
        tabData.child_materialsModel = eachMaterialsModelJsonValue.childCategory;

        // eslint-disable-next-line no-undef
        var key = guid();
        tagDataMap.put(key, tabData);

        var current = ''
        if (i == 0) {
            current = 'current';
        }

    }
    return tagDataMap
}
function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
const new_tree = (data) => {

    if (data != null && data.length > 0) {
        // 为子树增加code
        var materialsModelStack = new stack();
        for (var i = 0; i < data.length; i++) {
            data[i].key = guid();
            data[i].children = data[i].childCategory;
            data[i].title = data[i].categoryName
            // data[i].categoryName=name
            materialsModelStack.push(data[i]);
        }

        while (materialsModelStack.length() > 0) {
            var rootMaterialsModel = materialsModelStack.pop();


            // 如果现实数组中没有则加入到显示数组中
            var isLeaf = false;
            for (var i = 0; i < data.length; i++) {
                if (data[i].key == rootMaterialsModel.key) {
                    isLeaf = true;
                    break;
                }
            }
            if (!isLeaf) {
                data.push(rootMaterialsModel);
            }

            if (rootMaterialsModel.childCategory != null) {
                // let key;
                for (var i = 0; i < rootMaterialsModel.childCategory.length; i++) {
                    rootMaterialsModel.childCategory[i].key = guid();
                    rootMaterialsModel.childCategory[i].parentKey = rootMaterialsModel.key;
                    rootMaterialsModel.childCategory[i].title = rootMaterialsModel.childCategory[i].categoryName
                    if (i > 1) materialsModelStack.push(rootMaterialsModel.childCategory[i]);
                }
            }
        }
        return materialsModelStack.dataStore
    }
}
const second_Tree_fun = (data) => {
    if (data != null && data.length > 0) {
        // 为子树增加code
        var materialsModelStack = new stack();

        for (var i = 0; i < data.length; i++) {
            data[i].key = guid();
            data[i].children = data[i].ChildCategory;
            data[i].title = data[i].CategoryName
            // data[i].categoryName=name
            materialsModelStack.push(data[i]);
        }


        while (materialsModelStack.length() > 0) {
            var rootMaterialsModel = materialsModelStack.pop();

            // 如果现实数组中没有则加入到显示数组中
            var isLeaf = false;
            for (var i = 0; i < data.length; i++) {
                if (data[i].key == rootMaterialsModel.key) {
                    isLeaf = true;
                    break;
                }
            }
            if (!isLeaf) {
                data.push(rootMaterialsModel);
            }

            if (rootMaterialsModel.CategoryName != null) {
                // let key;
                for (var i = 0; i < rootMaterialsModel.ChildCategory.length; i++) {
                    rootMaterialsModel.ChildCategory[i].key = guid();
                    rootMaterialsModel.ChildCategory[i].parentKey = rootMaterialsModel.key;
                    rootMaterialsModel.ChildCategory[i].title = rootMaterialsModel.ChildCategory[i].CategoryName
                    if (i > 1) materialsModelStack.push(rootMaterialsModel.ChildCategory[i]);
                }
            }
        }
        return materialsModelStack.dataStore
    }
}
const child_treeVal = (data, fun) => {


    if (data.length == 1) {
        if (data[0].Properties != undefined) {

            if (data[0].Properties[0].Json_value.TableDataList != null) {
                let newData = test(data[0].Properties[0].Json_value.TableDataList)

                fun != undefined ? fun(newData) : console.log();
            } else {
                fun != undefined ? fun(data) : console.log();
            }
        } else {
            if (data[0].json_value.tableDataList != null) {
                let newData = test(data[0].json_value.tableDataList)
                fun != undefined ? fun(newData) : console.log();
            }
        }
    } else {
        fun != undefined ? fun(data) : console.log();
    }
}

const test = (data) => {

    function getColumns(jsonTableList) {
        // 得到第一行数据，进行表头的判断
        var columns = [];

        // 开始遍历表头
        for (var dataColIndex = 0; dataColIndex < jsonTableList.length; dataColIndex++) {
            var rowDataList = jsonTableList[dataColIndex];
            // 开始遍历每一列

            for (var dataRowIndex = 0; dataRowIndex < rowDataList.length; dataRowIndex++) {
                var eachRowData = rowDataList[dataRowIndex]
                var column = {};
                column.title = eachRowData.TipsName != undefined ? eachRowData.TipsName : eachRowData.tipsName;
                column.dataIndex = 'row_' + dataRowIndex;
                column.key = 'row_' + dataRowIndex;
                column.width = 150;
                column.render = (text, record) => {
                    if (text != '' && text != undefined) {
                        return text.indexOf('.jpg') > 0 || text.indexOf('.png') > 0 ? (
                            <img src={text} />
                        ) : (
                                text.indexOf('.pdf') > 0 || text.indexOf('.html') > 0 ? (
                                    <a href={text}>{text}</a>
                                ) : (
                                        text
                                    )
                            );
                    } else {
                        return '暂无数据'
                    }
                }

                columns.push(column);
            }

            // 只看第一行
            break;
        }

        return columns;
    }

    function getData(jsonTableList) {
        var dataSources = [];
        var dataSource = {};

        for (var dataColIndex = 0; dataColIndex < jsonTableList.length; dataColIndex++) {
            var rowDataList = jsonTableList[dataColIndex];
            // 开始遍历每一列

            for (var dataRowIndex = 0; dataRowIndex < rowDataList.length; dataRowIndex++) {
                var eachRowData = rowDataList[dataRowIndex]

                var variableName = 'row_' + dataRowIndex;
                dataSource[variableName] = eachRowData.Input_value != undefined ? eachRowData.Input_value : eachRowData.input_value;


            }

        }
        dataSources.push(dataSource);
        return dataSources
    }
    var analysisJson = data
    // 得到表头
    var columns = getColumns(analysisJson);


    // 得到数据
    var dataSources = getData(analysisJson);
    let newarr = [];

    var iiiiii = 0;
    return {
        columns,
        dataSources
    }


}
//回车事件
const onkeyDown=(e,fn)=>{
   if(e.keyCode===13){
       fn()
   }
}
export {
    get_function,
    edit_function,
    search_function,
    tree_fun,
    tree_new_fun,
    creat_tree,
    new_map,
    new_tree,
    child_treeVal,
    second_Tree_fun,
    test,
    onkeyDown
}